<?php
if (!defined('IN_TTAE')) exit('Access Denied');


//互译无线短信发送类
include_once ROOT_PATH . 'inc/api/apiBase.class.php';

class api_huyi extends apiBase {
    public $debug = true;
    private $APIID = 'cf_calfpeng';
    private $PWD= 'Olive740200';
    private $APIKEY = '8e342fe27db3e5dd2797a9a6665cda3f';

    public function __construct() {
        //$this->use_huyi();
    }

    function use_huyi() { // TODO 在后台管理界面设置参数
        /*global $_G;
        if($_G['setting']['sms_status'] != 1){
            msg('系统未开启发送短信功能');
        }
        if($_G['setting']['sms_appkey'] && $_G['setting']['sms_secrekey']){
              $_G['TOP']->appkey =  trim($_G['setting']['sms_appkey']);
              $_G['TOP']->secretKey = trim($_G['setting']['sms_secrekey']);
        }else{
            msg('短信appkey未配置,无法进行操作');
        }*/
    }

    function save_code($phone) {
        $key = 'sms_' . $phone;
        $code = $this->random(4, 1);
        memory('set', $key, $code, 600);    //10分钟有效
        return $code;
    }

    //发送验证码
    public function send($phone) {
        global $_G;
        $this->use_huyi();

        $key2 = 'count_' . $phone;
        $count = intval(memory('get', $key2));
        if ($count > 5) return  '短信发送过于频繁,请于15分钟后再发送';
        $count++;
        memory('set', $key2, $count, 60 * 15);

        //短信接口地址
        $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
        //获取手机号
        $mobile = $phone;
        //生成的随机数
        $mobile_code = $this->save_code($phone);;
        if(empty($mobile)){
            return '手机号码不能为空';
        }

        $post_data = "account=".$this->APIID."&password=".$this->APIKEY."&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$mobile_code."。请不要把验证码泄露给其他人。");
        //用户名是登录用户中心->验证码短信->产品总览->APIID
        //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
        $xml = $this->Post($post_data, $target);
        $gets =  $this->xml_to_array($xml);

        if($gets['code'] == 2) {
            return true;
        }

        return $gets['msg'];
    }
    /*
        <SubmitResult xmlns="http://106.ihuyi.cn/">
            <code>405</code>
            <msg>用户名或密码不正确(c)</msg>
            <smsid>0</smsid>
        </SubmitResult>
    */


    public function check($phone, $code, $is_del = false) {
        global $_G;
        $key = 'sms_' . $phone;
        $org_code = memory('get', $key);
        $rt = '验证码校验失败';
        if ($code == $org_code) $rt = true;
        if ($is_del && true === $rt) $this->del($phone);
        return $rt;
    }


    function del($phone) {
        $key = 'sms_' . $phone;
        memory('delete', $key);
    }

    function get($rs) {
    }

    function parse($rs) {
    }

    //random() 函数返回随机整数。
    function random($length = 6 , $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }

    //请求数据到短信接口，检查环境是否 开启 curl init。
    function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);

        return $return_str;
    }

    //将 xml数据转换为数组格式。
    function xml_to_array($xml){
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        /*$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";

        $arr = array();
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
                $subxml= $matches[2][$i];
                $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] = xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }

        return $arr;*/
    }
}

?>